package com.education.wisdom.controller.app;

import com.education.common.pojo.SysUserOther;
import com.education.common.utils.ApiReturnObject;
import com.education.wisdom.feign.UserFeign;
import com.education.wisdom.pojo.PaymentRecord;
import com.education.wisdom.service.IPaymentRecordService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static com.education.common.core.BaseController.writeJSON;

/**
 * 支付记录 信息操作处理
 *
 * @author ruoyi
 * @date 2018-11-20
 */

@RestController
@CrossOrigin
@Slf4j
@Api(tags = {"支付记录"})
@RequestMapping("/appPaymentRecord")
public class AppPaymentRecordController {

    @Autowired
    private IPaymentRecordService paymentRecordService;
    @Autowired
    private UserFeign userFeign;

    /**
     * 微信支付创建订单记录
     *
     * @param userId
     * @param amount
     * @param response
     * @throws IOException
     */
    @PostMapping("/insert")
    public void precreate1(String userId, Double amount, HttpServletResponse response) throws IOException {
        //log.info(LogUtil.LogInfo("微信支付获取订单号", new Date()));
        SysUserOther sysUserOther = userFeign.selectOnePCF(userId);
        if (sysUserOther != null) {
            String userName = sysUserOther.getSysRelname();
            String out_trade_no = String.valueOf(System.nanoTime());
            BigDecimal bl = new BigDecimal(amount);
            PaymentRecord record = new PaymentRecord();
            record.setRecordId(out_trade_no);
            record.setCreateTime(new Date());
            record.setUserId(userId);
            record.setPayerName(userName);
            record.setPayerUserNo(userId);
            record.setPayWayName("微信");
            //达人币充值
            record.setTrxType("2");
            record.setPayerPayAmount(bl);
            record.setOrderAmount(bl);
            paymentRecordService.insertPaymentRecord(record);
            writeJSON(out_trade_no, response, "200", "success");
        }
    }

    /**
     * 安卓查找充值达人币交易明细
     *
     * @param userId
     * @param trxType
     * @return
     */
    @GetMapping("/expensesRecord")
    public ApiReturnObject expensesRecord(String userId, String trxType) {
        PaymentRecord paymentRecord = new PaymentRecord();
        paymentRecord.setUserId(userId);
        paymentRecord.setTrxType(trxType);
        List<PaymentRecord> paymentRecords = paymentRecordService.selectPaymentRecordList(paymentRecord);
        return new ApiReturnObject("200", "查询成功", paymentRecords);
    }

}


